package com.ziyun.erp.modules.capital.service.impl;

import com.ziyun.erp.common.e.DeleteEnum;
import com.ziyun.erp.modules.capital.vo.SupplierApplyCashRuleVo;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;

import com.ziyun.erp.modules.capital.dao.SupplierApplyCashRuleDao;
import com.ziyun.erp.modules.capital.entity.SupplierApplyCashRuleEntity;
import com.ziyun.erp.modules.capital.service.SupplierApplyCashRuleService;


@Service("supplierApplyCashRuleService")
public class SupplierApplyCashRuleServiceImpl extends ServiceImpl<SupplierApplyCashRuleDao, SupplierApplyCashRuleEntity> implements SupplierApplyCashRuleService {

    @Autowired
    private SellerInfoService sellerInfoService;

    @Override
    public R queryPageApplyCashRuleList(Map<String, Object> params) {
        int totalCount = this.baseMapper.countApplyCashRule(params);
        int current = Integer.valueOf((String)params.get("page"));
        int size = Integer.valueOf((String)params.get("limit"));
        params.put("offset", (current - 1) * size);
        params.put("limit", size);
        List<SupplierApplyCashRuleVo> list = this.baseMapper.queryPageApplyCashRuleList(params);

        List<SellerInfoEntity> sellerInfoList = sellerInfoService.queryFactoryList();

        R r = R.ok();
        r.put("page", new PageUtils(list, totalCount, size, current));
        r.put("userData", sellerInfoList);
        return r;
    }

    @Override
    public R updateSupplierApplyCashRule(SupplierApplyCashRuleEntity supplierApplyCashRule, Long userId) {
        // 日结天数要大于1
        if("2".equals(supplierApplyCashRule.getRuleType()) && supplierApplyCashRule.getRuleDay() <= 0){
            return R.error("日结天数要大于1");
        }
        Integer ruleId = supplierApplyCashRule.getRuleId();
        if (ruleId == null){
            // 判断工厂是否已经添加了提现规则
            List<SupplierApplyCashRuleEntity> supplierApplyCashRuleEntityList = this.selectList(new EntityWrapper<>(new SupplierApplyCashRuleEntity())
                    .eq("factory_id", supplierApplyCashRule.getFactoryId())
            );
            if(supplierApplyCashRuleEntityList.size() > 0){
                return R.error("工厂已添加了提现规则");
            }
            supplierApplyCashRule.setCreateName(userId.toString());
            supplierApplyCashRule.setCreateTime(new Date());
            this.insert(supplierApplyCashRule);
        }else {
            supplierApplyCashRule.setUpdateName(userId.toString());
            supplierApplyCashRule.setUpdateTime(new Date());
            this.updateById(supplierApplyCashRule);
        }
        return R.ok();
    }

    @Override
    public R deleteApplyCashRule(List<Integer> integers) {
        Map<String, Object> updateMap = new HashMap<>();
        updateMap.put("delete", DeleteEnum.DELETE.toInt());
        this.baseMapper.updateBatchByMap(updateMap, integers);

        return R.ok();
    }
}
